/**
 * 
 */
package handler;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import com.google.common.base.Charsets;

/**
 * @author Kyniek
 *
 */
public class kynObjectTypeHandler extends BaseTypeHandler
{
	
	private static boolean debug = false;
	

	/* (non-Javadoc)
	 * @see org.apache.ibatis.type.BaseTypeHandler#getNullableResult(java.sql.ResultSet, java.lang.String)
	 */
	@Override
	public Object getNullableResult(ResultSet rs, String columnName) throws SQLException
	{
		if(debug)
		{
			System.out.println("getNullableResult RS " + columnName);
		}
		return null;
	}

	
	/* (non-Javadoc)
	 * @see org.apache.ibatis.type.BaseTypeHandler#getNullableResult(java.sql.CallableStatement, int)
	 */
	@Override
	public Object getNullableResult(CallableStatement cs, int i) throws SQLException
	{
		if(debug)
		{
			System.out.println("getNullableResult CS");
		}
		return null;
	}

	
	/* (non-Javadoc)
	 * @see org.apache.ibatis.type.BaseTypeHandler#setNonNullParameter(java.sql.PreparedStatement, int, java.lang.Object, org.apache.ibatis.type.JdbcType)
	 */
	@Override
	public void setNonNullParameter(PreparedStatement ps, int num, Object par, JdbcType type) throws SQLException
	{
		if(debug)
		{
			System.out.println("setNonNullParameter");
		}
	}

	
	@Override
	public Object getResult(CallableStatement cs, int columnIndex) throws SQLException
	{
		// TODO Auto-generated method stub
		return super.getResult(cs, columnIndex);
	}

	
	@Override
	public Object getResult(ResultSet rs, String columnName) throws SQLException
	{
		// TODO Auto-generated method stub
		return super.getResult(rs, columnName);
	}

	
	@Override
	public void setParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException
	{		
		if(parameter == null)
		{			
			if(debug)
			{
				System.err.println("NULL SETTING");
			}
			ps.setNull(i, Types.NULL );
			return;
		}
		if(debug)
		{
			System.out.println("setParameter : " + parameter.toString() + " type : " + parameter.getClass().getCanonicalName());
		}
		if(parameter instanceof Integer)
		{
			ps.setInt(i, (Integer)parameter);
		}
		else
		{
			//ps.setBytes(i, parameter.toString().getBytes(Charsets.UTF_8) );
			ps.setString(i, parameter.toString());
		}
		//super.setParameter(ps, i, parameter.toString(), jdbcType);
	}


	@Override
	public Object getNullableResult(ResultSet arg0, int arg1) throws SQLException
	{
		// TODO Auto-generated method stub
		return null;
	}

}
